<?php

error_reporting("E_ALL^E_NOTICE");
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: masuk.php");
    exit();
}
include './class.upload.php';
include './function/Barang.php';
include './function/Pengguna.php';
include './function/Gambar.php';

//deklarasi data barang
$barang = new Barang();
$pengguna = new Pengguna();
$gambar = new Gambar();

//cek data barang
if ($_POST['kabupaten'] == '') {
    $_SESSION['uploadbarang']['kabupaten'] = "kabupaten masih kosong";
} else if ($pengguna->cekKabupaten($_POST['kabupaten']) == "tidak") {
    $_SESSION['uploadbarang']['kabupaten'] = "kabupaten tidak ada";
} else if (!$pengguna->updateID_kabupaten($pengguna->getIDKabupaten($_POST['kabupaten']), $_SESSION['user']['status']) == "sukses") {
    $_SESSION['uploadbarang']['kabupaten'] = "gagal update kabupaten";
}

if ($_POST['alamat'] == '') {
    $_SESSION['uploadbarang']['alamat'] = "alamat masih kosong";
} else if (!$pengguna->updateAlamat($_POST['alamat'], $_SESSION['user']['status']) == "sukses") {
    $_SESSION['uploadbarang']['alamat'] = "gagal update alamat";
}

if ($_POST['telepon'] == '') {
    $_SESSION['uploadbarang']['telepon'] = "kategori masih kosong";
} else if (!$pengguna->updateNo_Telp($_POST['telepon'], $_SESSION['user']['status']) == "sukses") {
    $_SESSION['uploadbarang']['telepon'] = "gagal update telepon";
}

if ($_POST['kategori'] == 'Kategori *') {
    $_SESSION['uploadbarang']['kategori'] = "kategori masih kosong";
}

if ($_POST['namaBarang'] == '') {
    $_SESSION['uploadbarang']['namabarang'] = "nama barang masih kosong";
}

if ($_POST['deskripsi'] == '') {
    $_SESSION['uploadbarang']['deskripsi'] = "deskripsi masih kosong";
}

//cek upload foto
$image1 = false;
$image2 = false;
$image3 = false;
$tipe = array('image/jpeg', 'image/gif', 'image/png');
if (empty($_FILES["uploadFile"]["name"]) && empty($_FILES["uploadFile2"]["name"]) && empty($_FILES["uploadFile3"]["name"])) {
    $_SESSION['uploadbarang']['gambar'] = "Foto belum dimasukan";
} else {
    if (!empty($_FILES["uploadFile"]["name"])) {
        if (!in_array($_FILES["uploadFile"]["type"], $tipe)) {
            $_SESSION['uploadbarang']['gambar'] = "Maaf, Foto harus memiliki format .jpg , .gif atau .png";
        } else if ($_FILES["uploadFile"]["size"] > 3000000) {
            $_SESSION['uploadbarang']['gambar'] = "Ukuran file foto terlalu besar";
        } else {
            $image1 = true;
        }
    }
    if (!empty($_FILES["uploadFile2"]["name"])) {
        if (!in_array($_FILES["uploadFile2"]["type"], $tipe)) {
            $_SESSION['uploadbarang']['gambar'] = "Maaf, Foto harus memiliki format .jpg , .gif atau .png";
        } else if ($_FILES["uploadFile2"]["size"] > 3000000) {
            $_SESSION['uploadbarang']['gambar'] = "Ukuran file foto terlalu besar";
        } else {
            $image2 = true;
        }
    }
    if (!empty($_FILES["uploadFile3"]["name"])) {
        if (!in_array($_FILES["uploadFile3"]["type"], $tipe)) {
            $_SESSION['uploadbarang']['gambar'] = "Maaf, Foto harus memiliki format .jpg , .gif atau .png";
        } else if ($_FILES["uploadFile3"]["size"] > 3000000) {
            $_SESSION['uploadbarang']['gambar'] = "Ukuran file foto terlalu besar";
        } else {
            $image3 = true;
        }
    }
}

//proses upload barang
if (isset($_SESSION['uploadbarang'])) {
    header("Location: uploadBarang.php");
    exit;
} else {
    //deklarasi data barang
    $barang->setNama_Barang($_POST['namaBarang']);
    $barang->setDeskripsi($_POST['deskripsi']);
    $barang->setId_Kategori($_POST['kategori']);
    $barang->setId_Provinsi($pengguna->getIDProvinsi($pengguna->getIDKabupaten($_POST['kabupaten'])));
    $barang->setId_Pengguna($pengguna->getIDFromEmail($_SESSION['user']['status']));
    $barang->setStatus('belum');
    $id_barang1 = $barang->getId_Barang();
    $gambar->setid_barang($id_barang1);
    $id_gambar1 = $gambar->getid_gambar();

    //upload foto & insert database
    if ($image1 == true) {
        $temp = explode(".", $_FILES["uploadFile"]["name"]);
        $newfilename = $id_gambar1;
        $filethumb = $id_gambar1 . "_s";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            //insert barang to database barang
            if ($barang->insert_Barang() == 'sukses') {
                //url gambar asli dan tumbnail sementara disamakan
                $gambar->seturl_gambar_asli("http://semuaberbagi.com/imageBarang/" . $newfilename . "." . end($temp));
                $gambar->seturl_gambar_tumbnail("http://semuaberbagi.com/imageBarang/thumb/" . $filethumb . "." . end($temp));
                //insert foto to database gambar
                if ($gambar->insertgambar() == 'sukses') {
                    $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
                } else {
                    $_SESSION['uploadbarang']['gambar'] = "foto 1 tidak tersimpan";
                }
            } else {
                $_SESSION['uploadbarang']['statusupload'] = "Barang gagal diunggah";
            }
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 1 gagal";
        }
    }
    if ($image1 == true && $image2 == true) {
        $temp = explode(".", $_FILES["uploadFile2"]["name"]);
        $newfilename = $id_gambar1 . "_2";
        $filethumb = $id_gambar1 . "_s2";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile2"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile2"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 2 gagal";
        }
    } else if ($image1 == false && $image2 == true) {
        $temp = explode(".", $_FILES["uploadFile2"]["name"]);
        $newfilename = $id_gambar1;
        $filethumb = $id_gambar1 . "_s";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile2"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile2"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            //insert barang to database barang
            if ($barang->insert_Barang() == 'sukses') {
                //url gambar asli dan tumbnail sementara disamakan
                $gambar->seturl_gambar_asli("http://semuaberbagi.com/imageBarang/" . $newfilename . "." . end($temp));
                $gambar->seturl_gambar_tumbnail("http://semuaberbagi.com/imageBarang/thumb/" . $newfilename . "." . end($temp));
                //insert foto to database gambar
                if ($gambar->insertgambar() == 'sukses') {
                    $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
                } else {
                    $_SESSION['uploadbarang']['gambar'] = "foto 2 tidak tersimpan";
                }
            } else {
                $_SESSION['uploadbarang']['statusupload'] = "Barang gagal diunggah";
            }
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 2 gagal";
        }
    }
    if ($image1 == true && $image2 == true && $image3 == true) {
        $temp = explode(".", $_FILES["uploadFile3"]["name"]);
        $newfilename = $id_gambar1 . "_3";
        $filethumb = $id_gambar1 . "_s3";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile3"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile3"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 3 gagal";
        }
    } else if ($image1 == true && $image2 == false && $image3 == true) {
        $temp = explode(".", $_FILES["uploadFile3"]["name"]);
        $newfilename = $id_gambar1 . "_2";
        $filethumb = $id_gambar1 . "_s2";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile3"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile3"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 3 gagal";
        }
    } else if ($image1 == false && $image2 == true && $image3 == true) {
        $temp = explode(".", $_FILES["uploadFile3"]["name"]);
        $newfilename = $id_gambar1 . "_2";
        $filethumb = $id_gambar1 . "_s2";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile3"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile3"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 3 gagal";
        }
    } else if ($image1 == false && $image2 == false && $image3 == true) {
        $temp = explode(".", $_FILES["uploadFile3"]["name"]);
        $newfilename = $id_gambar1;
        $filethumb = $id_gambar1 . "_s";
        //upload file foto & hasil resize
        $foo1 = new upload($_FILES["uploadFile3"]);
        $foo1->file_new_name_body = $newfilename;
        $foo1->image_convert = end($temp);
        $foo1->process("imageBarang/");
        $foo = new upload($_FILES["uploadFile3"]);
        $foo->file_new_name_body = $filethumb;
        $foo->image_convert = end($temp);
        $foo->image_resize = TRUE;
        $foo->image_x = 150;
        $foo->image_y = 150;
        $foo->process("imageBarang/thumb/");
        if ($foo1->processed && $foo->processed) {
            //insert barang to database barang
            if ($barang->insert_Barang() == 'sukses') {
                //url gambar asli dan tumbnail sementara disamakan
                $gambar->seturl_gambar_asli("http://semuaberbagi.com/imageBarang/" . $newfilename . "." . end($temp));
                $gambar->seturl_gambar_tumbnail("http://semuaberbagi.com/imageBarang/thumb/" . $newfilename . "." . end($temp));
                //insert foto to database gambar
                if ($gambar->insertgambar() == 'sukses') {
                    $_SESSION['uploadbarang']['statusupload'] = "Barang berhasil diunggah";
                } else {
                    $_SESSION['uploadbarang']['gambar'] = "foto 3 tidak tersimpan";
                }
            } else {
                $_SESSION['uploadbarang']['statusupload'] = "Barang gagal diunggah";
            }
        } else {
            $_SESSION['uploadbarang']['gambar'] = "Upload foto 3 gagal";
        }
    }

    header("Location: uploadBarang.php");
    exit;
}